package day1;

import day1.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.Util;

import java.util.List;
import java.util.Set;

public class TestApp {
    SqlSession session;
    public static void main(String[] args)throws Exception {
        //用下面对象来读取配置文件
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //用这个对象来获取一个sqlSession对象
        SqlSessionFactory factory=builder.build(Resources.getResourceAsStream("day1/my-config.xml"));
        //用session进行数据库的操作
        SqlSession session=factory.openSession();
        Object o=session.selectOne("day1.dao.UserDao.selectById",1);
        List list=session.selectList("day1.dao.UserDao.findByName","%a%");
        System.out.println(list);
        session.close();
    }

    @Test
    public void insert(){
        User u=new User();
        u.setName("tom");
        u.setPass("abc");
        u.setPhone("139");
        u.setAddress(("西安"));
        session.insert("day1.dao.UserDao.insertUser",u);
        System.out.println(u);

    }
    @Before
    public void init(){
        session= Util.getSession();
    }
    @After
    public void close(){
        session.commit();
        session.close();
    }

    @Test
    public void update(){
        User u=new User();
        u.setName("hello");
        u.setId(1);
        session.update("day1.dao.UserDao.updateUser",u);
    }
    @Test
    public void delete(){
        session.delete("day1.dao.UserDao.deleteUser",4);
    }
}
